package tech.wangtong.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import tech.wangtong.pojo.User;
import tech.wangtong.util.UpDownUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Controller
public class InoutController {
    //前提一:MultipartFile file(和前端name一致)
    @RequestMapping("/upload")
        public String upload(MultipartFile file,HttpServletRequest req){
        String filename = UpDownUtil.upload(file, req);
        req.setAttribute("filename",filename);
        return "/day2/upload.jsp";
    }
    //下载文件：只需要在当前页面下载数据，不需要跳转
    //返回值不能是String，需要返回ResponseEntity
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(String filename,HttpServletRequest req){
       return UpDownUtil.download(filename,req);
    }

    //springmvc只需要添加形参,
    //要求是形参的名称和形参类型与方法参数名称和类型一致
    //如果类型不一致,springmvc还会自动类型转换
    @RequestMapping("/one")
    public String oneParam(String name,Integer id){
        System.out.println("name"+name);
        System.out.println("id"+id);
        return "/index.jsp";
    }
    @RequestMapping("/delAll")
    public String delAll(Integer[] ids){
        Arrays.stream(ids).forEach(System.out::println);
        return "/index.jsp";
    }
    //400错误：是属于客户端异常，前台提交的数据和厚度按接收
    //数据类型不一致，如果无法正常转换，出现错误的(日期类型和文件类型)
    @RequestMapping("/likeAll")
    public String likeAll(String[] likes,@DateTimeFormat(pattern = "yyyy-MM-dd") Date time){
        Arrays.stream(likes).forEach(System.out::println);
        System.out.println(time);
        return "/index.jsp";
    }
    //springmvc可以自动封装对象,前提对象的属性名和前台提交的参数名一致
    //底层先借助封装好的BeanWrapper类,再借助反射机制,将参数名封装到对象中
    //如果对象中存在数组,数组名必须和前台的数组名一致
    //再使用setXXX()进行属性赋值
    @RequestMapping("/addUser")
    public String addUser(User u){
        System.out.println(u.getName()+" "+u.getId()+" "+u.getPassword()+" "+u.getSex()+" "+u.getStatus()+" "+u.getTime());
        return "/index.jsp";
    }

    //存储request作用域
    @RequestMapping("/show1")
    public String show1(HttpServletRequest req,
                        ModelAndView mv,
                        Model m1,
                        ModelMap m2,
                        Map m3){
        req.setAttribute("key1","wangtong1");
        mv.addObject("key2","wangtong2");//需要mv.setViewName("/day1/show.jsp"),然后返回ModelAndView对象
        m1.addAttribute("key3","wangtong3");
        m2.addAttribute("key4","wangtong4");
        m3.put("key5","wangtong5");

        return "/day1/show.jsp";
    }
}
